package unteractivities.p3_fitnessapp;

import java.util.ArrayList;
import java.util.List;
import model.p3_fitnessapp.MuscleGroup;
import model.p3_fitnessapp.MySQLiteHelper;
import model.p3_fitnessapp.TrainingPlan;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import controler.p3_fitnessapp.ActTrainingplan;
import controler.p3_fitnessapp.R;


/**
 * 
 * @author tcockx
 * 
 */
public class UActTPChooseMuscleGroup extends Activity {

	/*
	 * ==========================================================================
	 * Globale Variablen
	 * ========================================================
	 */
	ArrayList<MuscleGroup> listMusclegroup = new ArrayList<MuscleGroup>();
	List<String> tpListMg = new ArrayList<String>();
	Intent intent = new Intent();

	
	/*
	 * ==========================================================================
	 * Android Methoden ========================================================
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.uact_new_trainingplan_choose_musclegroup);

        
		}
	

	
	@Override
	protected void onResume() {

		super.onResume();
	
		/*
		 * Adapter einstellen, ListView finden
		 */
		ArrayAdapter<String> ap_Mg = new ArrayAdapter<String>(
		UActTPChooseMuscleGroup.this, android.R.layout.simple_list_item_1, tpListMg);
		
		MySQLiteHelper db_trainingsplan = new MySQLiteHelper(getApplicationContext());
		
		
		/*
		 * Methodenvariablenj
		 */
		int z = 0;
		
		Intent intentTp = getIntent();
		int tp_Id = intentTp.getIntExtra("tp_id", -1);
		intent.putExtra("tp_id", tp_Id);
		if (tp_Id == -1) {
			return;
		}
		else {
			/*
			 * Clear ArrayList 
			 */
			listMusclegroup.clear();
			
			Log.d(null, + tp_Id +" tp ID");
			listMusclegroup = db_trainingsplan.getMuscleGroupByTrainingplanID(tp_Id);
		}
		
	        tpListMg.clear();
		
		for (MuscleGroup tp : listMusclegroup) {
			String tp_data = tp.getName();
			Log.d(null, "Muskelgruppe: " + tp_data);
			tpListMg.add(tp_data);
			tp.setList_id(z);
			z++;
		}
		
        ListView tp_ListView = (ListView) findViewById(R.id.listViewMg);
        tp_ListView.setAdapter(ap_Mg);
        
		tp_ListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				int i = 0;

				/*
				 * Loop ��ber den Trainingsplan Wenn die gew��hlte ID gleich der
				 * eingegebenen ist, wird sie ausgegeben ansonsten wird die ID 1
				 * erh��ht
				 */

				intent.setClassName(getApplicationContext().getPackageName(),
						"unteractivities.p3_fitnessapp.UActTPChooseExercise");

				// intent.putExtra("selected",
				// tpListView.getAdapter().getItem(arg2).toString());

				// intent.putExtra("test", "test");

				for (MuscleGroup mg : listMusclegroup) {

					if (mg.getList_id() == arg2) {

						Log.d(null, "arg 2 gleich");


					Log.d(null, "ID " + mg.getId());
					Log.d(null, "listID " + mg.getList_id());
						intent.putExtra("mg_id", mg.getId());

					} else {
						Log.d(null, "arg2 ungleich");
					}
					i++;
				}

				startActivity(intent);

			}
		});
	}

	/*
	 * ==========================================================================
	 * Benutzer Interaktion (Buttons ... )
	 * ========================================================
	 */

	/*
	 * ==========================================================================
	 * Eigene Methoden ========================================================
	 */

	/*
	 * ==========================================================================
	 * Debug Methoden ========================================================
	 */

	/*
	 * ==========================================================================
	 * Getter und Setter
	 * ========================================================
	 */

}
